package com.qdxwx.web.controller.ajax;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.picture.PictureAlbumService;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.picture.PictureAlbum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/ajax/picture/album")
public class AjaxPictureAlbumController {

    @Autowired
    private PictureAlbumService pictureAlbumService;

    @GetMapping("/get.json")
    @ResponseBody
    public AjaxResult view(HttpServletRequest request){
        int id = TKConvert.toInteger(request.getParameter("id"));
        if(id < 1) return AjaxResult.error("参数异常");
        PictureAlbum pictureAlbum = pictureAlbumService.get(id);
        if(pictureAlbum == null) return AjaxResult.error("找不到指定素材");
        return AjaxResult.success(pictureAlbum);
    }

    @GetMapping("/list.json")
    @ResponseBody
    public AjaxResult list(HttpServletRequest request){
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        int categoryId = TKConvert.toInteger(request.getParameter("categoryId"));
        int subjectId = TKConvert.toInteger(request.getParameter("subjectId"));
        Integer elite = TKConvert.toInteger(request.getParameter("elite"),null);
        Integer isTop = TKConvert.toInteger(request.getParameter("isTop"),null);
        Long userId = TKConvert.toLong(request.getParameter("userId"));
        int page = TKConvert.toInteger(request.getParameter("page"),1);
        int limit = TKConvert.toInteger(request.getParameter("limit"),10);
        PictureAlbum pictureQuery = new PictureAlbum();
        pictureQuery.setChannelId(channelId);
        pictureQuery.setIsTop(isTop);
        pictureQuery.setIsElite(elite);
        pictureQuery.setSubjectId(subjectId);
        pictureQuery.setCategoryId(categoryId);
        pictureQuery.setStatus(1);
        pictureQuery.setUserId(userId);
        PageRequest pageRequest = new PageRequest();
        pageRequest.setCurrentPage(page);
        pageRequest.setPageSize(limit);
        List<PictureAlbum> pictureList = pictureAlbumService.getList(pictureQuery, pageRequest);
        return AjaxResult.success(pictureList);
    }
}
